package log

import (
	"context"

	"github.com/illidaris/core"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

type RestLogger struct {
	log *zap.Logger
}

func NewRestLogger() *RestLogger {
	return &RestLogger{
		log: zap.L().WithOptions(zap.AddCallerSkip(1)),
	}
}

func (l *RestLogger) DebugCtx(ctx context.Context, msg string) {
	l.log.Debug(msg, WithTrace(ctx)...)
}

func (l *RestLogger) InfoCtx(ctx context.Context, msg string) {
	l.log.Info(msg, WithTrace(ctx)...)
}

func (l *RestLogger) WarnCtx(ctx context.Context, msg string) {
	l.log.Warn(msg, WithTrace(ctx)...)
}

func (l *RestLogger) ErrorCtx(ctx context.Context, msg string) {
	l.log.Error(msg, WithTrace(ctx)...)
}

func WithTrace(ctx context.Context) []zapcore.Field {
	traceID := core.TraceID.GetString(ctx)
	sessionID := core.SessionID.GetString(ctx)

	return []zapcore.Field{
		zap.String(core.TraceID.String(), traceID),
		zap.String(core.SessionID.String(), sessionID),
	}
}
